package com.example.hospital.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alipay.api.domain.AmpeCategoryInfo;
import com.example.hospital.api.common.PageUtils;
import com.example.hospital.api.common.R;
import com.example.hospital.api.controller.from.*;
import com.example.hospital.api.service.DoctorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @FileName DoctorController
 * @folder
 * @Description
 * @Author yf
 * @return
 * @date 2024-08-19
 **/
@RestController
@RequestMapping("/doctor")
@Api(tags = "医生管理")
public class DoctorController {
    @Resource
    DoctorService doctorService;
    /**
    *
    * @folder 分页查询医生
    * @Param: [form]
    * @Author: yf
    * @return: com.example.hospital.api.common.R
    */
    @ApiOperation("分页查询医生")
    @PostMapping("/searchByPage")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","DOCTOR:SELECT"},mode = SaMode.OR)
    public R searchByPage(@RequestBody @Valid SearchDoctorByPageForm form){
        Map param = BeanUtil.beanToMap(form);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page-1)*length;
        param.put("start",start);
        PageUtils pageUtils = doctorService.searchByPage(param);
        return R.ok().put("result",pageUtils);
    }
    /**
    *
    * @folder 查询医生内容
    * @Param: [form]
    * @Author: yf
    * @return: com.example.hospital.api.common.R
    */

    @ApiOperation("查询医生内容")
    @PostMapping("/searchContent")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","DOCTOR:SELECT"},mode = SaMode.OR)
    public R searchContent(@RequestBody @Valid SearchDoctorContentFrom form){
        HashMap map = doctorService.searchContent(form.getId());
        return R.ok(map);
    }

    /**
    *
    * @folder 更新医生头像
    * @Param:
    * @Author: yf
    * @return:
    */
    @ApiOperation("更新医生头像")
    @PostMapping("/updatePhoto")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","DOCTOR:UPDATE"},mode = SaMode.OR)
    public R updatePhoto(@Param("file")MultipartFile file,@Param("doctorId")Integer doctorId){
        doctorService.updatePhoto(file,doctorId);
        return R.ok();
    }
    /**
    *
    * @folder 插入医生信息
    * @Param: [param]
    * @Author: yf
    * @return: void
    */

    @ApiOperation("插入医生信息")
    @PostMapping("/insert")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","DOCTOR:INSERT"},mode = SaMode.OR)
    public R insert(@RequestBody @Valid InsertDoctorForm form){
        Map param = BeanUtil.beanToMap(form);
        String json = JSONUtil.parseArray(form.getTag()).toString();
        param.replace("tag",json);
        param.put("uuid", IdUtil.simpleUUID().toUpperCase());
        doctorService.insert(param);
        return R.ok();
    }
    @ApiOperation("查询医生信息")
    @PostMapping("/searchById")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","DOCTOR:SELECT"},mode = SaMode.OR)
    public R searchById(@RequestBody SearchDoctorByIdForm form){
        HashMap map = doctorService.searchById(form.getId());
        return R.ok(map);
    }
    @ApiOperation("更新医生信息")
    @PostMapping("/update")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","DOCTOR:UPDATE"},mode = SaMode.OR)
    public R update(@RequestBody @Valid UpdateDoctorForm form){
        Map map = BeanUtil.beanToMap(form);
        String json = JSONUtil.parseArray(form.getTag()).toString();
        map.replace("tag",json);
        doctorService.update(map);
        return R.ok();
    }
    @ApiOperation("删除医生信息")
    @PostMapping("/deleteByIds")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","DOCTOR:DELETE"},mode = SaMode.OR)
    public R delete(@RequestBody @Valid deleteDoctorByIdsFrom form){
        doctorService.deleteDoctorByIds(form.getIds());
        return R.ok();
    }
}
